layui.config({
    base: '../../js/address'
}).extend({
    address: 'address',
});

layui.define(["form", "jquery"], function (exports) {
    var form = layui.form(),
        $ = layui.jquery,
        Address = function () {
        };

    Address.prototype.provinces = function () {
        //加载省数据
        var proHtml = '', that = this;
        $.ajax({
            type: 'GET',
            url: "/region",
            data: {parent_id: 0, type: 1},
            dataType: "json",
            complete: function (XHR) {
                XHR = null;
            },
            error: function (xhr) {//获取ajax的错误信息
                layer.alert(xhr.responseText, "Failed",function () {
                    layer.closeAll();
                });
            },
            success: function (response) {
                if (response.code == 403) {
                    layer.msg(response.msg, {icon: 4, time: 3000})
                    return false;
                }
                if (response.code != 200) {
                    layer.msg(response.msg, {icon: 5, time: 3000});
                    return false;
                }
                var pro = response.data;
                for (var i = 0; i < pro.length; i++) {
                    proHtml += '<option value="' + pro[i].id + '">' + pro[i].name + '</option>';
                }
                //初始化省数据
                $("select[name=province]").append(proHtml);
                form.render();
                form.on('select(province)', function (proData) {
                    $("select[name=area]").html('<option value="">请选择县/区</option>');
                    var value = proData.value;
                    if (value > 0) {
                        $.ajax({
                            type: 'GET',
                            url: "/region",
                            data: {parent_id: value, type: 2},
                            dataType: "json",
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed",function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000});
                                    return false;
                                }
                                that.citys(res.data);
                            }
                        });
                    } else {
                        $("select[name=city]").attr("disabled", "disabled");
                    }
                });
            }
        });
    }

    //加载市数据
    Address.prototype.citys = function (citys) {
        var cityHtml = '<option value="">请选择市</option>', that = this;
        for (var i = 0; i < citys.length; i++) {
            cityHtml += '<option value="' + citys[i].id + '">' + citys[i].name + '</option>';
        }
        $("select[name=city]").html(cityHtml).removeAttr("disabled");
        form.render();
        form.on('select(city)', function (cityData) {
            var value = cityData.value;
            if (value > 0) {
                $.ajax({
                    type: 'GET',
                    url: "/region",
                    data: {parent_id: value, type: 3},
                    dataType: "json",
                    complete: function (XHR) {
                        XHR = null;
                    },
                    error: function (xhr) {//获取ajax的错误信息
                        layer.alert(xhr.responseText, "Failed",function () {
                            layer.closeAll();
                        });
                    },
                    success: function (response) {
                        if (response.code == 403) {
                            layer.msg(response.msg, {icon: 4, time: 3000})
                            return false;
                        }
                        if (response.code != 200) {
                            layer.msg(response.msg, {icon: 5, time: 3000});
                            return false;
                        }
                        var pro = response.data;
                        that.areas(pro);
                    }
                });
            } else {
                $("select[name=area]").attr("disabled", "disabled");
            }
        });
    }

    //加载县/区数据
    Address.prototype.areas = function (areas) {
        var areaHtml = '<option value="">请选择县/区</option>';
        for (var i = 0; i < areas.length; i++) {
            areaHtml += '<option value="' + areas[i].id + '">' + areas[i].name + '</option>';
        }
        $("select[name=area]").html(areaHtml).removeAttr("disabled");
        form.render();
    }

    var address = new Address();
    exports("address", function () {
        address.provinces();
    });
});
